package com.jiang.demo.common.util;

import com.jiang.demo.config.knife4j.Knife4jDisplayEnum;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * ---------------------------------------------------------------------------------------------------------------
 * - 2024-06-01 15:24 - jiangshaoneng -
 * ---------------------------------------------------------------------------------------------------------------
 * -      ██╗██╗ █████╗ ███╗   ██╗ ██████╗ ███████╗██╗  ██╗ █████╗  ██████╗ ███╗   ██╗███████╗███╗   ██╗ ██████╗
 * -      ██║██║██╔══██╗████╗  ██║██╔════╝ ██╔════╝██║  ██║██╔══██╗██╔═══██╗████╗  ██║██╔════╝████╗  ██║██╔════╝
 * -      ██║██║███████║██╔██╗ ██║██║  ███╗███████╗███████║███████║██║   ██║██╔██╗ ██║█████╗  ██╔██╗ ██║██║  ███╗
 * - ██   ██║██║██╔══██║██║╚██╗██║██║   ██║╚════██║██╔══██║██╔══██║██║   ██║██║╚██╗██║██╔══╝  ██║╚██╗██║██║   ██║
 * - ╚█████╔╝██║██║  ██║██║ ╚████║╚██████╔╝███████║██║  ██║██║  ██║╚██████╔╝██║ ╚████║███████╗██║ ╚████║╚██████╔╝
 * -  ╚════╝ ╚═╝╚═╝  ╚═╝╚═╝  ╚═══╝ ╚═════╝ ╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═══╝╚══════╝╚═╝  ╚═══╝ ╚═════╝
 * ---------------------------------------------------------------------------------------------------------------
 * ---------------------------------------------------------------------------------------------------------------
 */
public class ReflectUtil {

    /**
     * 获取类所有字段，包括父类字段
     * @param origin 类
     * @return Field[]
     */
    public static Field[] getAllDeclaredFields(Class<?> origin){
        Class<?> currentClass = origin;
        List<Field> fieldList = new ArrayList<>();
        while (currentClass != null) {
            Field[] fields = currentClass.getDeclaredFields();
            fieldList.addAll(Arrays.asList(fields));
            currentClass = currentClass.getSuperclass();
        }
        return fieldList.toArray(new Field[0]);
    }

    /**
     * 根据枚举类名(全路径)获取swagger枚举描述信息
     * @param notes 全类名
     * @param value 字段名
     * @return swagger枚举描述
     */
    public static String joinText(String notes, String value){
        Class<?> rawPrimaryType;
        try {
            rawPrimaryType = Class.forName(notes);
        } catch (ClassNotFoundException e) {
            return "";
        }
        Object[] subItemRecords = null;
        Knife4jDisplayEnum knife4jDisplayEnum = AnnotationUtils.findAnnotation(rawPrimaryType, Knife4jDisplayEnum.class);
        if (null != knife4jDisplayEnum && Enum.class.isAssignableFrom(rawPrimaryType)) {
            subItemRecords = rawPrimaryType.getEnumConstants();
        }
        if (null == subItemRecords) {
            return "";
        }
        final List<String> displayValues = Arrays.stream(subItemRecords).filter(Objects::nonNull)
                .map(Object::toString).filter(Objects::nonNull).collect(Collectors.toList());

        String joinText = " [" + String.join(", ", displayValues) + "]";
        try {
            joinText = "<p>["+ notes+"]<p>" + value +":"+ joinText +"<p>";
        } catch (Exception ignore) {
        }
        return joinText;
    }
}
